package cn.wolfcode.p2p.controller;

import cn.wolfcode.p2p.base.domain.IpLog;
import cn.wolfcode.p2p.base.domain.LoginInfo;
import cn.wolfcode.p2p.base.domain.SystemDictionaryItem;
import cn.wolfcode.p2p.base.domain.UserInfo;
import cn.wolfcode.p2p.base.exception.CustomException;
import cn.wolfcode.p2p.base.service.ISystemDictionaryService;
import cn.wolfcode.p2p.base.service.IUserInfoService;
import cn.wolfcode.p2p.util.JSONResult;
import cn.wolfcode.p2p.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class BasicInfoController {
        @Autowired
        private IUserInfoService userInfoService;
        @Autowired
        private ISystemDictionaryService systemDictionaryService;


        @RequestMapping("basicInfo")
        public String list(Model model){
                LoginInfo loginInfo = UserContext.getLoginInfo();
                UserInfo userinfo = userInfoService.select(loginInfo.getId());
                model.addAttribute("userinfo",userinfo);
                List<SystemDictionaryItem> educationBackgrounds = systemDictionaryService.selectBySn("educationBackgrounds");
                model.addAttribute("educationBackgrounds",educationBackgrounds);
                model.addAttribute("incomeGrades",systemDictionaryService.selectBySn("incomeGrades"));
                model.addAttribute("marriages",systemDictionaryService.selectBySn("marriages"));
                model.addAttribute("kidCounts",systemDictionaryService.selectBySn("kidCounts"));
                model.addAttribute("houseConditions",systemDictionaryService.selectBySn("houseConditions"));
                return "userInfo";
        }
        @RequestMapping("basicInfo_save")
        @ResponseBody
        public JSONResult basicInfosave(UserInfo userInfo){
                JSONResult jsonResult = new JSONResult();
                try {
                        userInfoService.basicInfosave(userInfo);
                }catch (CustomException e ){
                        jsonResult.setMsg(e.getMessage());//自定义的异常,避免所有异常都返回同样的信息给客户
                } catch (Exception e) {
                        e.printStackTrace();
                        jsonResult.setMsg("系统繁忙,请稍后再试");
                }
                return jsonResult;



        }

}
